<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>点击显示下拉式菜单</title>
    <link rel="stylesheet" href="./style.css">
</head>

<body>
    <div id="app" v-cloak>
        <div class="main" v-clickoutside="handleClose">
            <button @click="show = !show">点击显示下拉式菜单</button>
            <div class="dropdown" v-show="show">
                <p>个人信息</p>
                <p>下午，有课</p>
                <p>你好，退出</p>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="./index.js"></script>
    <script src="./clickoutside.js"></script>
    <script>
        Vue.directive('clickoutside', {
            bind: function(el, binding, vnode) {
                function documentHandler(e) {
                    if (el.contains(e.target)) {
                        return false;
                    }

                    if (binding.expression) {
                        binding.value(e);
                    }
                }
                el.__vueClickOutside__ = documentHandler;
                document.addEventListener('click', el.__vueClickOutside__)
            },
            unbind: function(el, binding) {
                document.removeEventListener('click', el.__vueClickOutside__)
                delete el.__vueClickOutside__;
            }
        })
        let app1 = new Vue({
            el: '#app',
            data() {
                return {
                    show: false
                }
            },
            methods: {
                handleClose: function() {
                    this.show = false
                }
            }
        })
    </script>
</body>

</html>